package callnumber.gtdev5.com.photoVault.customview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

import com.gtdev5.geetolsdk.mylibrary.util.DensityUtils;

/**
 * Created by cheng
 * PackageName FakeCall
 * 2018/6/25 15:56
 */

@SuppressLint("AppCompatCustomView")
public class VivoView2 extends ImageView{
    //设置wrap_content时候View的大小
    private int defaultWidth = 100;
    private int defaultHeight = 100;
    private int beginX,beginY;
    private int oneceTop;
    private int oneceBottom;
    private int oneceLeft;
    private int oneceRight;
    private boolean isMeasure=false;
    private boolean isOnceTouch=true;
    private float firstX; //控件初始X坐标
    private float firstY;//控件初始Y坐标
    public ViVOViewScrolllitener viVOViewScrolllitener;
    public VivoView2(Context context) {
        super(context);
    }
    public VivoView2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    public VivoView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x= (int) event.getX();
        int y= (int) event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                beginX=x;
                beginY=y;
                firstX=getX();
                firstY=getY();
                if (!isMeasure){
                    oneceTop=getTop();
                    oneceBottom=getBottom();
                    oneceLeft=getLeft();
                    oneceRight=getRight();
                }
                isMeasure=true;
                break;
            case MotionEvent.ACTION_MOVE :
                //计算拖动距离
                int moveX=x-beginX;
                int moveY=y-beginY;
                int px=  DensityUtils.Dp2Px(getContext(),70);
               // if (getY()>600 && getY()<1350 && getX()>40 && getX()<780){  //设定上下左右滑动的边界值
                    layout(getLeft()+moveX,getTop()+moveY,getRight()+moveX,getBottom()+moveY);
                    if (getY()<firstY-px*2){
                        if (isOnceTouch){
                            viVOViewScrolllitener.onTopScrollListener();
                            isOnceTouch=false;
                        }

                    }
                    if ( getY()>firstY+px*2){
                        if (isOnceTouch){
                            viVOViewScrolllitener.onBottomScrollListener();
                            isOnceTouch=false;
                        }


                    }
               // }
                Log.e("TAG","move_x"+getX()+"   move_y:"+getY());
                break;
            case MotionEvent.ACTION_UP:
                layout(oneceLeft,oneceTop,oneceRight,oneceBottom);
                break;
        }
        return true;
    }
    //简单的重写onMeasure
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        if(widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(defaultWidth,defaultHeight);
        }else if(widthSpecMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(defaultWidth,heightSpecSize);
        }else if(heightSpecMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(widthSpecSize, defaultHeight);
        }else{
            setMeasuredDimension(widthSpecSize, heightSpecSize);
        }
    }

   public void setViVOViewScrolllitener (ViVOViewScrolllitener viVOViewScrolllitener){
        this.viVOViewScrolllitener=viVOViewScrolllitener;
   }

    public interface ViVOViewScrolllitener{
        void onTopScrollListener();
        void onBottomScrollListener();
    }
}
